package creative.fire.interview.ca;

public class BigIntPlus {
	public String plus(String sa, String sb) {
		char[] a = sa.toCharArray();
		char[] b = sb.toCharArray();
		StringBuilder sc = new StringBuilder();

		final int alen = a.length;
		final int blen = b.length;
		int len = alen;
		if (len < blen)
			len = blen;

		int[] ss = new int[len];

		int modnumber = 0;
		int divnumber = 0;
		for (int i = 1; i <= len; i++) {
			final int ia = alen - i >= 0 ? a[alen - i] - '0' : 0;
			final int ib = blen - i >= 0 ? b[blen - i] - '0' : 0;
			final int sum = ia + ib + divnumber;
			divnumber = sum / 10;
			modnumber = sum % 10;
			ss[len - i] = modnumber;
		}

		if (divnumber > 0)
			sc.append(divnumber);

		for (int i = 0; i < ss.length; i++) {
			sc.append(ss[i]);
		}
		return sc.toString();
	}

	final int[] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999, 99999999, 999999999, Integer.MAX_VALUE };

	// Requires positive x
	public int stringSizeOfInt(int x) {
		for (int i = 0;; i++)
			if (x <= sizeTable[i])
				return i + 1;
	}

	public int stringSizeOfLong(long x) {
		long p = 10;
		for (int i = 1; i < 19; i++) {
			if (x < p)
				return i;
			p = 10 * p;
		}
		return 19;
	}

	public static void main(String[] args) {
		BigIntPlus me = new BigIntPlus();
		System.out.println(me.plus("123", "77"));
		System.out.println(me.stringSizeOfLong(123456789123456789l));
	}
}
